import listStatus from '../../tabBar/public/js/way-list-status.js';
import uploadImage from '@/js_sdk/yushijie-ossutil/ossutil/uploadFile.js';
export default {
	data() {
		return {
			waybillDetail: {},
			filesArr: [],
			imageArr: [],
			remake: '',
			imageStyles: {
				width: 84,
				height: 84,
				border: {
					color: "#e2e2e2",
					width: 2,
					style: 'dashed',
					radius: '2px'
				}
			},
		}
	},
	created() {

	},
	onLoad(options) {
		// #ifdef APP-PLUS
		var obj = JSON.parse(options.obj);
		// #endif
		
		// #ifdef MP-WEIXIN
		var obj = JSON.parse(decodeURIComponent(options.obj));
		// #endif
		this.waybillDetail = obj;
	},
	onShow() {

	},
	methods: {
		getSelect(row) {
			let d = listStatus.getSelect(row);
			console.log(d)
			return d;
		},
		waybillStatus(rows) {
			return listStatus.status(rows);
		},
		getStatusP2(rows) {
			return listStatus.getStatusP2(rows);
		},
		getLevel(rows) {
			return listStatus.getLevel(rows);
		},
		getStatus(rows) {
			return listStatus.getStatus(rows);
		},
		getTransitStatus(rows) {
			return listStatus.getTransitStatus(rows);
		},
		getDestinationCity(rows) {
			return listStatus.getDestinationCity(rows);
		},
		getDisabledDel(rows) {
			return listStatus.getDisabledEdit(rows);
		},
		getDisabledEdit(rows) {
			return listStatus.getDisabledEdit(rows);
		},
		// 获取上传状态
		select(e) {
			this.filesArr.push(e.tempFilePaths[0])
		},
		deleteImg(e) {
			for (var i = 0; i < this.filesArr.length; i++) {
				var item = this.filesArr[i];
				if (e.tempFilePath == item) {
					this.filesArr.splice(i, 1)
				}
			}
		},
		// 上传失败
		fail(e) {
			console.log('上传失败：', e)
		},
		uploadImage() {
			if (this.filesArr.length > 0) {
				uni.showModal({
					title: '是否要上传照片',
					content: '是否继续操作',
					confirmText: "确定",
					success: (res) => {
						if (res.confirm) {
							this.selectFile();
						}
					}
				})
			} else {
				uni.showToast({
					icon: 'fail',
					title: '请拍照上传'
				})
			}
		},
		submitImage() {
			if (this.imageArr.length > 0) {
				this.submitRequest();
			} else {
				uni.showToast({
					icon: 'fail',
					title: '请拍照上传'
				})
			}
		},

		submitRequest() {
			var data = {
				waybill_id: this.waybillDetail.waybill_id,
				img_url: this.imageArr.join(','),
			};
			if (this.$refs.loadingCover) this.$refs.loadingCover.show();
			this.http.sendRequest({
				api: this.api.order.deliverytCertificate,
				data: data,
				success: res => {
					console.log(res)
					if (res.code == '1' && res.data) {
						uni.showModal({
							title: '凭证提交成功',
							confirmText: "确定",
							success: (res) => {
								if (res.confirm) {
									uni.navigateBack()
								}
							}
						})
					} else {
						this.$util.showToast({
							title: res.msg
						});
					}
					if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
				},
				fail: res => {
					if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
				}
			});
		},
		selectFile() {
			this.imageArr = [];
			for (var i = 0; i < this.filesArr.length; i++) {
				let date = new Date;
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDay();
				// 保存路径
				let filePath = "3pl/depart/" + year + "-" + month + "-" + day + "/";
				// 调用上传函数
				this.fileUpload(this.filesArr[i], filePath);
			}
		},
		/* 上传函数 */
		fileUpload(path, stroeAs) {
			return new Promise((resolve, reject) => {
				uni.showLoading({
					title: '上传中'
				})
				uploadImage(path, stroeAs,
					result => {
						// 成功上传后的OSS服务器图片地址放到图片列表中并显示
						this.imageArr.push(result)
						console.log(this.imageArr);
						uni.hideLoading();
					},
					fail => {
						uni.hideLoading();
					}
				)
			}).catch((e) => {})
		}
		// departCertificate
	}
}